package com.training.api.controller;

import com.training.base.Result;
import com.training.api.service.FeedbackService;
import com.training.bean.Feedback;
import com.training.jwt.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService service;

    /**
     * 添加意见反馈
     * @param content 反馈内容
     * @return 操作结果
     */
    @PostMapping("/add")
    public ResponseEntity<Result<Feedback>> addFeedback(@RequestParam String content, HttpServletRequest request) {
        try {
            // 从JWT token中获取用户ID
            int uid = JWTUtils.getUserIdFromRequest(request);
            Feedback feedback = service.saveFeedback(uid, content);
            return ResponseEntity.ok(new Result<>(feedback, true, "意见反馈提交成功"));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(Result.fail("意见反馈提交失败：" + e.getMessage()));
        }
    }
}